<template>
	<view class="index">
		<image src="../../static/img/read-card.gif" class="read-card"></image>
		<view class="plr32">
			<view class="mb40">
				<xc-button name="搜索蓝牙" @click="searchBlueTooth"></xc-button>
			</view>
			<!-- <xc-tip :tipArr="tipArr"></xc-tip> -->
		</view>
	</view>
</template>

<script>
	import xcTip from "@/components/simple/xcTip.vue"
	import xcButton from "@/components/simple/xcButton.vue"
	import bluetooth from "@/static/utils/bluetooth.js"
	import {throttle} from "@/static/utils/utils.js"
	export default {
		components: {
			xcTip,
			xcButton
		},
		onload() {
			let that = this;
			//获取手机定位信息
			uni.getLocation({
				success: () => {
					const system = uni.getSystemInfoSync()
					if (!system.locationEnabled) {
						util.showModal('请先开启手机定位，否则可能无法搜索蓝牙')
					}
				},
				complete() {
					that.setLocationAuth();
				}
			})
			that.valideSysInfo();
		},
		onShow(){
			 //重新初始化数据和蓝牙，否则可能搜索不到蓝牙
			bluetooth.closeBluetooth();
			this.initData();
		},
		data() {
			return {
				tipArr: ['1、请将IC卡芯片朝上插入卡槽，点击搜索蓝牙按钮', '2、连接成功蓝灯亮']
			}
		},
		methods: {
			initData(){
				let globalData = getApp().globalData;
				    globalData.deviceId = "";
				    globalData.iSAllowDcovere = true; //是否允许搜索，防止重复搜索
				    globalData.onValueFunStr = "getCardType"; //监听完成后需要执行的函数回调
				    globalData.currentcardData = {}; //当前卡对象数据
				    globalData.currentCommandsIndex = 0; //当前指令的索引
				    globalData.cardCommands = ["121200000000000000000000001C010C049782"]; //指令命令
				    globalData.returnCommands = []; //根据指令返回的报文信息
				    globalData.writeCardCardData = {};
				    globalData.readCardType = "";
				    globalData.isWriting = false;
			},
			searchBlueTooth:throttle(function(){
				this.$navigateTo("/pages/bluetooth/connectDevice");
			}),
			//验证微信版本是否支持微信小程序
			valideSysInfo() {
				let sysinfo = this.getSystemInfoSync();
				if (sysinfo.platform == 'android' && this.versionCompare('6.5.7', sysinfo.version)) {
					this.$showModal("当前微信版本过低，请更新至最新版本");
				} else if (sysinfo.platform == 'ios' && this.versionCompare('6.5.6', sysinfo.version)) {
					this.$showModal("当前微信版本过低，请更新至最新版本");
				}
			},
			//版本比较
			versionCompare: function(ver1, ver2) {
				var version1pre = parseFloat(ver1)
				var version2pre = parseFloat(ver2)
				var version1next = parseInt(ver1.replace(version1pre + ".", ""))
				var version2next = parseInt(ver2.replace(version2pre + ".", ""))
				if (version1pre > version2pre)
					return true
				else if (version1pre < version2pre)
					return false
				else {
					if (version1next > version2next)
						return true
					else
						return false
				}
			},
			//获取平台信息
			getSystemInfoSync: function() {
				let systemInfo = uni.getSystemInfoSync();
				return {
					platform: systemInfo.platform,
					version: systemInfo.version
				}
			},
			//是否授权获取位置信息
			setLocationAuth() {
				uni.getSetting({
					success: (res) => {
						let statu = res.authSetting;
						if (!statu['scope.userLocation']) {
							this.$showModal('需要获取您的地理位置，请确认授权，否则可能无法搜索到蓝牙设备', () => {
								uni.openSetting({
									success: (data) => {}
								})
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.index {
		.read-card {
			width: 750upx;
			height: 589upx;
			margin-top: 120upx;
		}
	}
</style>
